package com.sj.dto;

import com.sj.config.Constance;
import com.sj.util.JsonMapper;

import java.util.HashMap;
import java.util.Map;

/**
 * TODO  json返回对象
 *
 * @author liufeng
 * @create 2017/8/5 14:39
 */
public class Result  {

    private String code;
    private Boolean ok;
    private String msg;
    private Map<String,Object> content = new HashMap<>(1<<2);

    private Result(String code, Boolean ok, String msg) {
        this.code = code;
        this.ok = ok;
        this.msg = msg;
    }

    public static Result error() {
        return new Result(Constance.EXCEPTION, Boolean.FALSE , "操作失败");
    }

    public static Result success() {
        return new Result(Constance.SUCCESS, Boolean.TRUE, "操作成功");
    }

    public String getCode() {
        return code;
    }

    public Result setCode(String code) {
        this.code = code;
        return this;
    }

    public Boolean getOk() {
        return ok;
    }

    public Result setOk(Boolean ok) {
        this.ok = ok;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Object getContent() {
        return content;
    }

    public Result with(String key, Object value) {
        content.put(key,value);
        return this;
    }


    @Override
    public String toString() {
        return JsonMapper.nonEmptyMapper().toJson(this);
    }
}
